// 引入 QCloud 小程序增强 SDK
var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => wx.showToast({
    title: text,
    icon: 'success'
});

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};


Page({
    data: {
        latitude: 0,
        longitude: 0,
        address: '',
        addressTitles: [],
        requestUrl: config.service.getAddressTitleListUrl
    },
    onLoad: function (options) {
        console.log("latitude:" + options.latitude + " longitude:" + options.longitude);
        this.setData({
            latitude: options.latitude,
            longitude: options.longitude
        })
    },
    onShow: function () {
        var that = this;
        wx.showToast({
            title: '加载数据',
            duration: 2000
        });

        // api.address.list(this.data.latitude, this.data.longitude, function(state, result){
        //     console.log(result);
        //     that.setData({
        //         addressTitles: result
        //     });
        //     wx.hideToast()
        // });
        var data = {
            latitude: this.data.latitude,
            longitude: this.data.longitude,
        };
        qcloud.request({
            // 要请求的地址
            url: this.data.requestUrl,
            data: data,

            // 请求之前是否登陆，如果该项指定为 true，会在请求之前进行登录
            login: false,

            success(result) {
                showSuccess('请求成功完成');
                console.log('request success', result);
                // var userInfo = result.data.data.userInfo;
                // wx.setStorageSync('userInfo', userInfo);
                var list = result.data;
                // var list = JSON.parse(data);
                that.data.addressTitles = list;
                that.setData({
                    addressTitles: list
                });
            },
            fail(error) {
                showModel('请求失败', error);
                console.log('request fail', error);
            },
            complete() {
                console.log('request complete');
            }
        });

    },
    bindInputAddress: function (e) {
        var content = e.detail.value;
        this.setData({
            address: content
        });
    },
    setChooseAddressTitle: function () {
        // app.globalData.chooseAddressTitle = this.data.address;
        wx.setStorageSync('locationTitle', this.data.address);
        wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
        })
    },
    chooseAddressIndex: function (e) {
        var that = this;
        var obj = e.currentTarget;
        var index = obj.dataset.index;
        if (index >= 0) {
            var indexData = that.data.addressTitles[index];
            var title = indexData.address
            that.setData({
                address: title
            });
        }
        console.log(1);


    }

});